<?php

require_once 'vendor/autoload.php';

use App\Models\SystemLog;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

// 设置Laravel环境
$app = require_once 'bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

echo "开始创建测试日志数据...\n";

try {
    // 获取现有用户
    $users = User::all();

    if ($users->isEmpty()) {
        echo "没有找到用户，请先创建用户数据\n";
        exit(1);
    }

    $userIds = $users->pluck('id')->toArray();
    $userNames = $users->pluck('username')->toArray();

    // 测试数据
    $testLogs = [
        [
            'user_id' => $userIds[0] ?? null,
            'action' => 'login',
            'module' => 'auth',
            'description' => '用户登录系统',
            'level' => 'info',
            'ip_address' => '192.168.1.100',
            'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'created_at' => Carbon::now()->subMinutes(30)
        ],
        [
            'user_id' => $userIds[0] ?? null,
            'action' => 'create',
            'module' => 'user',
            'target_type' => 'User',
            'target_id' => 2,
            'description' => '创建新用户: test_user',
            'level' => 'info',
            'ip_address' => '192.168.1.100',
            'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'created_at' => Carbon::now()->subMinutes(25)
        ],
        [
            'user_id' => null,
            'action' => 'backup',
            'module' => 'system',
            'description' => '系统自动备份完成',
            'level' => 'info',
            'ip_address' => '127.0.0.1',
            'user_agent' => 'System Cron',
            'created_at' => Carbon::now()->subMinutes(20)
        ],
        [
            'user_id' => $userIds[0] ?? null,
            'action' => 'update',
            'module' => 'department',
            'target_type' => 'Department',
            'target_id' => 1,
            'description' => '更新部门信息: 技术部',
            'old_data' => ['name' => '技术部门', 'manager_id' => 1],
            'new_data' => ['name' => '技术部', 'manager_id' => 2],
            'level' => 'info',
            'ip_address' => '192.168.1.100',
            'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'created_at' => Carbon::now()->subMinutes(15)
        ],
        [
            'user_id' => $userIds[0] ?? null,
            'action' => 'delete',
            'module' => 'position',
            'target_type' => 'Position',
            'target_id' => 3,
            'description' => '删除职位: 实习生',
            'old_data' => ['name' => '实习生', 'department_id' => 2, 'salary' => 3000],
            'level' => 'warning',
            'ip_address' => '192.168.1.100',
            'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'created_at' => Carbon::now()->subMinutes(10)
        ],
        [
            'user_id' => null,
            'action' => 'error',
            'module' => 'system',
            'description' => '数据库连接超时',
            'level' => 'error',
            'ip_address' => '127.0.0.1',
            'user_agent' => 'System Monitor',
            'context' => ['error_code' => '2002', 'error_message' => 'Connection timed out'],
            'created_at' => Carbon::now()->subMinutes(5)
        ],
        [
            'user_id' => $userIds[0] ?? null,
            'action' => 'export',
            'module' => 'report',
            'description' => '导出员工报表',
            'level' => 'info',
            'ip_address' => '192.168.1.100',
            'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'created_at' => Carbon::now()->subMinutes(2)
        ],
        [
            'user_id' => $userIds[0] ?? null,
            'action' => 'permission_change',
            'module' => 'permission',
            'description' => '修改用户权限',
            'level' => 'warning',
            'ip_address' => '192.168.1.100',
            'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'created_at' => Carbon::now()->subMinute()
        ]
    ];

    $createdCount = 0;
    foreach ($testLogs as $logData) {
        SystemLog::create($logData);
        $createdCount++;
    }

    echo "成功创建 {$createdCount} 条测试日志数据\n";

    // 显示创建的日志统计
    $stats = SystemLog::selectRaw('level, COUNT(*) as count')
        ->groupBy('level')
        ->pluck('count', 'level')
        ->toArray();

    echo "日志统计:\n";
    foreach ($stats as $level => $count) {
        echo "- {$level}: {$count} 条\n";
    }

} catch (Exception $e) {
    echo "创建测试数据失败: " . $e->getMessage() . "\n";
    exit(1);
}

echo "测试数据创建完成！\n";
